package netdemo;

import java.io.*;
import java.net.Socket;

public class Client {

    public static void main(String[] args) {
        //socket是虚拟的终端，开启实现IO的虚拟接口
        try {
            Socket client = new Socket("localhost", 10086);
            OutputStream outputStream = client.getOutputStream();
            InputStream inputStream = client.getInputStream();
            //将输出流对象进行封装
            DataOutputStream dataOutputStream = new DataOutputStream(outputStream);
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
            BufferedReader con = new BufferedReader(new InputStreamReader(System.in));

            String info = null;
            while (!(info = con.readLine()).equals("结束")) {
                dataOutputStream.writeUTF(info);
                String readinfo = null;
                readinfo = bufferedReader.readLine();
                System.err.println(readinfo);
                if (readinfo.equals("结束")) {
                    client.shutdownInput();//添加流输入完成标志,调用shutdown告诉对方传输完成
                    break;
                }
            }
            if (info.equals("结束")) {
                dataOutputStream.writeUTF(info);
                client.shutdownOutput();//添加流输出完成标志
            }
            con.close();
            bufferedReader.close();
            dataOutputStream.close();
            inputStream.close();
            outputStream.close();
            client.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
